package com.liu.dao.impl;

import com.liu.dao.IRateDao;
import com.liu.pojo.Rate;
import com.liu.util.LoggerFactory;

import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 老刘Matthew
 * @Timestamp 2022/7/13 21:21
 * @Description
 */
public class RateDaoImpl extends GenericDaoImpl<Rate> implements IRateDao, LoggerFactory {

    @Override
    public List<Rate> query(Integer novel_id) throws SQLException {

        String sql =                        //
                "select " +                 //
                        "rate_id, " +       //
                        "user_name, "  +    //
                        "content " +        //
                    "from " +               //
                        "rate r " +         //
                    "join " +               //
                        "user u " +         //
                    "on " +                 //
                        "r.user_id = u.user_id " +  //
                    appendWhere("where r.novel_id = " + novel_id);

        return jdbcTemplate.query(sql, resultSet -> {

            List<Rate> list = new ArrayList<>();

            while (resultSet.next()) {

                Rate o = new Rate();
                o.setRate_id(resultSet.getInt("rate_id"));
                o.setUser_name(resultSet.getString("user_name"));
                o.setContent(resultSet.getString("content"));

                list.add(o);
            }
            return list;
        });

    }

    // 其他用户评论小说
    @Override
    public Integer save(Rate o) throws SQLException {

        String sql =
                "insert into " +    //
                        "rate(novel_id, content, user_id, push_time) " + //
                        "values(?, ?, ?, ?)";

        return jdbcTemplate.updateOrInsert(sql, //
                new Object[]{
                        o.getNovel_id(),
                        o.getContent(),
                        o.getUser_id(),
                        o.getPush_time()
                }, new Integer[]{
                        Types.INTEGER,
                        Types.VARCHAR,
                        Types.INTEGER,
                        Types.TIMESTAMP
                });
    }

    // 回复某个评论
    public Integer saveWithRevert_id(Rate o) throws SQLException {
        String sql = //
                "insert into " +    //
                        "rate(novel_id, content, revert_id, user_id, push_time) " + //
                        "values(?, ?, ?, ?, ?)";

        return jdbcTemplate.updateOrInsert(sql, //
                new Object[]{
                        o.getNovel_id(),
                        o.getContent(),
                        o.getRevert_id(),
                        o.getUser_id(),
                        o.getPush_time()
                }, new Integer[]{
                        Types.INTEGER,
                        Types.VARCHAR,
                        Types.INTEGER,
                        Types.INTEGER,
                        Types.TIMESTAMP
                });
    }

    @Override
    public Integer update(Rate rate) throws SQLException {
        return null;
    }

    @Override
    public List<Rate> query(String where) throws SQLException {
        return null;
    }

    @Override
    public List<Rate> queryAll() throws SQLException {
        return null;
    }

    @Override
    public Rate querySingle(Integer id) throws SQLException {
        return null;
    }

    @Override
    public Integer queryCount(String table, String where) throws SQLException {
        return null;
    }


}
